package com.ruoyi.web.controller.ta;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ta.domain.bo.TaProjectBo;
import com.ruoyi.ta.domain.vo.TaProjectVo;
import com.ruoyi.ta.service.ITaProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 项目Controller
 *
 * @author ruoyi
 * @date 2022-06-27
 */
@Validated
@Api(value = "项目控制器", tags = {"项目管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/ta/project")
public class TaProjectController extends BaseController {

    private final ITaProjectService iTaProjectService;

    /**
     * 查询项目列表
     */
    @ApiOperation("查询项目列表")
    @PreAuthorize("@ss.hasPermi('ta:project:list')")
    @GetMapping("/list")
    public TableDataInfo<TaProjectVo> list(@Validated(QueryGroup.class) TaProjectBo bo) {
        bo.setCreatedBy(getUsername());
        return iTaProjectService.queryPageList(bo);
    }

    /**
     * 导出项目列表
     */
    @ApiOperation("导出项目列表")
    @PreAuthorize("@ss.hasPermi('ta:project:export')")
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(@Validated TaProjectBo bo, HttpServletResponse response) {
        List<TaProjectVo> list = iTaProjectService.queryList(bo);
        ExcelUtil.exportExcel(list, "项目", TaProjectVo.class, response);
    }

    /**
     * 获取项目详细信息
     */
    @ApiOperation("获取项目详细信息")
    @PreAuthorize("@ss.hasPermi('ta:project:query')")
    @GetMapping("/{id}")
    public AjaxResult<TaProjectVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iTaProjectService.queryById(id));
    }

    /**
     * 新增项目
     */
    @ApiOperation("新增项目")
    @PreAuthorize("@ss.hasPermi('ta:project:add')")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody TaProjectBo bo) {
        bo.setCreatedBy(getUsername());
        bo.setCreatedTime(new Date());
        return toAjax(iTaProjectService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改项目
     */
    @ApiOperation("修改项目")
    @PreAuthorize("@ss.hasPermi('ta:project:edit')")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody TaProjectBo bo) {
        return toAjax(iTaProjectService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除项目
     */
    @ApiOperation("删除项目")
    @PreAuthorize("@ss.hasPermi('ta:project:remove')")
    @Log(title = "项目" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iTaProjectService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
